package com.senarai.tempedroid;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class Connection {
	private final static String webServicePrefix = "http://192.168.186.1:48156/Tempe/service/?operation=";
	
	public JSONObject getItem(int id) {
		String serviceURL = webServicePrefix + "barang.get&barang_id=" + id;
		String resultService = getWebServiceResult(serviceURL);
		System.out.println(serviceURL);
		System.out.println(resultService+"aaaaaaaaaa");
		
		JSONObject jsonObject = stringToJSONObject(resultService);
		System.out.println(serviceURL);
		System.out.println(resultService);
		System.out.println(jsonObject.toString());
		return jsonObject;
	}
	
	public JSONArray getAllItems(){
		String serviceURL = webServicePrefix + "barang.getAll";
		String resultService = getWebServiceResult(serviceURL);
		
		JSONArray jsonArray = stringToJSONArray(resultService);
		
		return jsonArray;
	}
	
	public JSONArray getItemsByTerm(String term) {
		String serviceURL = webServicePrefix + "barang.getAll&term=" + term;
		String resultService = getWebServiceResult(serviceURL);
		
		JSONArray jsonArray = stringToJSONArray(resultService);
		
		return jsonArray;
	}
	
	public JSONArray getItemsByCategory(int categoryID) {
		String serviceURL = webServicePrefix + "barang.getAll&kategori_id=" + categoryID;
		String resultService = getWebServiceResult(serviceURL);
		
		JSONArray jsonArray = stringToJSONArray(resultService);
		
		return jsonArray;	
	}
	
	public JSONObject getCategory(int id) {
		String serviceURL = webServicePrefix + "kategori.get&kategori_id=" + id;
		String resultService = getWebServiceResult(serviceURL);
	
		JSONObject jsonObject = stringToJSONObject(resultService);
		
		return jsonObject;
	}
	
	public JSONArray getAllCategories() {
		String serviceURL = webServicePrefix + "kategori.getAll";
		String resultService = getWebServiceResult(serviceURL);
		
		JSONArray jsonArray = stringToJSONArray(resultService);
		
		return jsonArray;
	}
	
	public JSONObject getUser(String username) {
		String serviceURL = webServicePrefix + "user.getInfo&username"+ username;
		String resultService = getWebServiceResult(serviceURL);
		
		JSONObject jsonObject = stringToJSONObject(resultService);
		
		return jsonObject;
	}
	
	public void orderItems(String username, String password, JSONArray jsonArrayItems) {
		
	}
	
	public void confirmPayment(String username, String password, int orderID) {
		
	}
	
	
	/* PRIMITIVE FUNCTION */
	/*--------------------*/
	public String getWebServiceResult(String webServiceURL) {
		
		InputStream is = null;
		String result = "";
		StringBuilder resultSB = null;
		
		HttpClient client = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(webServiceURL);
		
		try {
			System.out.println("try");
			HttpResponse response = client.execute(httpGet);
			StatusLine statusLine = response.getStatusLine();
			int statusCode = statusLine.getStatusCode();
			if (statusCode == 200){
				System.out.println("status code conn : 200");
				HttpEntity entity = response.getEntity();
				is = entity.getContent();
				// Convert response to string
				BufferedReader reader = new BufferedReader(new InputStreamReader(is));
				String line = null;
				resultSB = new StringBuilder();
				while ((line = reader.readLine()) != null) {
					resultSB.append(line);
				}
				is.close();
				result = resultSB.toString();
			} else{
				System.out.println("status code conn : not 200");
				Log.e(Connection.class.toString(), "Failed to download");
			}
		} catch(ClientProtocolException e) {
			System.out.println("catch 1"+e.toString());
			e.printStackTrace();
		} catch(IOException e){
			System.out.println("catch 2"+e.toString());
			e.printStackTrace();
		}
		return result;
	}

	public JSONObject stringToJSONObject(String input) {
		JSONObject jsonObject = null;
		//Parse String ke JSON Object
		try {
			jsonObject = new JSONObject(input);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return jsonObject;
	}
	
	public JSONArray stringToJSONArray(String input) {
		JSONArray jsonArray = null;
		//Parse String ke JSON Array
		try {
			jsonArray = new JSONArray(input);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return jsonArray;
	}
}
